package com.ydy.service.impl.center;

import com.ydy.enums.YesOrNo;
import com.ydy.mapper.ItemsCommentsMapper;
import com.ydy.mapper.OrderItemsMapper;
import com.ydy.mapper.OrderStatusMapper;
import com.ydy.mapper.OrdersMapper;
import com.ydy.pojo.OrderItems;
import com.ydy.pojo.OrderStatus;
import com.ydy.pojo.Orders;
import com.ydy.pojo.bo.center.OrderItemsCommentBO;
import com.ydy.pojo.vo.MyCommentVO;
import com.ydy.service.center.MyCommentsService;
import com.ydy.utils.PageInfo;
import com.ydy.utils.PageUtil;
import com.ydy.utils.Sid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MyCommentsServiceImpl implements MyCommentsService {

    public static final Logger log =
            LoggerFactory.getLogger(MyCommentsServiceImpl.class);

    @Autowired
    public OrderItemsMapper orderItemsMapper;

    @Autowired
    public OrdersMapper ordersMapper;

    @Autowired
    public OrderStatusMapper orderStatusMapper;

    @Autowired
    public ItemsCommentsMapper itemsCommentsMapper;

    @Autowired
    private Sid sid;

    /**
     * 查询未评论的订单
     * @param orderId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<OrderItems> queryPendingCommentItems(String orderId) {
        OrderItems query = new OrderItems();
        query.setOrderId(orderId);
        log.info("根据订单id:{}查询未评论商品",orderId);
        return orderItemsMapper.select(query);
    }

    /**
     * 保存评论
     * @param orderId 订单id
     * @param userId 用户id
     * @param commentList 评论商品集合
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveComments(String orderId, String userId,
                             List<OrderItemsCommentBO> commentList) {

        // 1. 保存评价 items_comments
        saveItemsComments(userId,commentList);

        // 2. 修改订单表改已评价 orders
        updateOrders(orderId);

        // 3. 修改订单状态表的留言时间 order_status
        updateOrderStatus(orderId);
    }

    /**
     * 保存评价
     * @param userId 用户iD
     * @param commentList 商品列表
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveItemsComments(String userId, List<OrderItemsCommentBO> commentList) {
        for (OrderItemsCommentBO oic : commentList) {
            oic.setCommentId(sid.nextShort());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("commentList", commentList);
        log.info("保存用户id:{}的评价",userId);
        itemsCommentsMapper.saveComments(map);
    }

    /**
     * 修改订单表改已评价
     * @param orderId 订单id
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateOrders(String orderId) {
        Orders order = new Orders();
        order.setId(orderId);
        order.setIsComment(YesOrNo.YES.type);
        log.info("更新订单id:{}的状态已评价",orderId);
        ordersMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * 修改订单状态表的留言时间
     * @param orderId 订单iD
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateOrderStatus(String orderId) {
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderId(orderId);
        orderStatus.setCommentTime(new Date());
        log.info("更新订单id:{}的状态的浏览时间",orderId);
        orderStatusMapper.updateByPrimaryKeySelective(orderStatus);
    }

    /**
     * 查询我的评论
     * @param userId 用户id
     * @param pageInfo 分页对象
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<MyCommentVO> queryMyComments(String userId, PageInfo pageInfo) {
        PageUtil.StartPage(pageInfo);
        List<MyCommentVO> list = itemsCommentsMapper.queryMyComments(userId);
        return list;
    }

}
